<?php

require('httpheader.php');
require('../install/mysqldb.php');
header('Content-Type:application/json; charset=utf-8');

if($_POST && $cin){
    mysqli_select_db($cin,"hongbao");
    date_default_timezone_set('PRC');
    $firstchoose = $_POST["firstchoose"];
    // $data = json_decode($_POST["data"]);
    $problemset = $_POST["problemset"];
    $allcount = 1 + count($problemset);
    if($_POST["spoof"]){
        $spoof = $_POST["spoof"];
        $allcount +=1;
    }
    $uid = $_POST["uid"][0];

    $userlist = mysqli_query($cin,"SELECT * FROM userinfo WHERE id=$uid");
    $result = array();
    $data = array();
    $score = 0;
    if($userlist->num_rows){
        while($row = $userlist->fetch_row()){
            $problem = json_decode($row[7]);
            if($firstchoose == $problem->firstchoose[1]){
                $score++ ;
            }
            if( isset($spoof) && $spoof == $problem->spoof[1]){
                $score++ ;
            }
            $answer = json_decode($row[8]);
            foreach($problemset as $key=>$value){
                if($value == $answer->problemset[$key]){
                    $score++ ;
                }else if(strpos($answer->problemset[$key], $value) !== false){
                    $score+=0.5 ;
                }
            }
            if(($score / $allcount)*100 > 60){
                $data = ["coding"=>$row[5]];
                
            }
            $data["points"] = ($score / $allcount)*100;
            $result["data"] = $data;
            $result["code"] = "200";
        }
        // echo json_encode($result);
        
    }
    echo json_encode($result);

}

?>